﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Windows;

using Prism.Ioc;

using DLL.Standard.Infrastructure;

using DLL.NET50.DB.PIVAS.ORM.Service;
using DLL.NET50.DB.PIVAS.Entitys;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.NET70.Infrastructure.Prism;
using DLL.NET60.DB.MS.Entity.DTO;
using DLL.Standard.Infrastructure.Log;
using DLL.NET50.DB.MS.Entity.VM;
using DLL.NET.DB.MS.Drugs.CRUD.Service;
using DLL.Standard.Infrastructure.Entity;

namespace PrismModule.EditDrugs.ViewModels
{
    public class PUC_ShowDicDrugsViewModel : CMVVMBase, IRegionMemberLifetime
    {
        #region navigate

        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        #endregion navigate

        #region window load

        public override void Loaded()
        {
            LogHelper.Debug("PUC_ShowDicDrugsViewModel  初始化");
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start(); //  开始监视代码
            //dicDrugs = _msDrugsService.GetDrugs();
            dicDrugs = _msDrugsService.GetDrugs(50);
            if (null != dicDrugs)
            {
                vmDrugs = VMChange.GetVM(dicDrugs);
            }

            LogHelper.Debug("用时=" + GetUsedTotalTime(stopwatch).ToString() + "ms");
        }

        public override void UnLoaded()
        {
            base.UnLoaded();
            LogHelper.Debug("退出 PUC_ShowDicDrugs");
        }

        #endregion window load

        #region property varaible

        private List<DTO_adi_dic_drug> _dicDrugs;

        public List<DTO_adi_dic_drug> dicDrugs
        {
            get { return _dicDrugs; }
            set
            {
                SetProperty(ref _dicDrugs, value);
            }
        }

        private DTO_adi_dic_drug _selectDicDrug;

        public DTO_adi_dic_drug selectDicDrug
        {
            get { return _selectDicDrug; }
            set
            {
                SetProperty(ref _selectDicDrug, value);
            }
        }

        private List<CVMDrug> _vmDrugs;

        public List<CVMDrug> vmDrugs
        {
            get { return _vmDrugs; }
            set
            {
                SetProperty(ref _vmDrugs, value);
            }
        }

        private CVMDrug _selectVMDrug;

        public CVMDrug selectVMDrug
        {
            get { return _selectVMDrug; }
            set
            {
                SetProperty(ref _selectVMDrug, value);
            }
        }

        private string _drugCode = "";

        public string drugCode
        {
            get { return _drugCode; }
            set
            {
                SetProperty(ref _drugCode, value);
            }
        }

        private string _pyCode = "";

        public string pyCode
        {
            get { return _pyCode; }
            set
            {
                SetProperty(ref _pyCode, value);
            }
        }

        private string _drugName = "";

        public string drugName
        {
            get { return _drugName; }
            set
            {
                SetProperty(ref _drugName, value);
            }
        }

        #endregion property varaible

        #region interface variable

        private IEventAggregator _eventAggregator;
        private IContainerExtension _container;
        private IMSDrugsService _msDrugsService;
        private IPivasDrugsService _pivasService;
        private IMSUpAndDownDrugService _upAndDownDrugService;

        #endregion interface variable

        public PUC_ShowDicDrugsViewModel(
            IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IApplicationCommands applicationCommands,
            IContainerExtension container,
            IMSDrugsService drugsService,
            IPivasDrugsService pivasDrugsService,
            IMSUpAndDownDrugService upAndDownDrugService)
            : base(regionManager, eventAggregator, applicationCommands)
        {
            Message = "MotionCardModule: View MotionCard from your Prism Module";
            _eventAggregator = eventAggregator;
            _container = container;
            _msDrugsService = drugsService;
            _pivasService = pivasDrugsService;
            _upAndDownDrugService = upAndDownDrugService;

            #region interface delegate command initialize

            AddCommand = new DelegateCommand(Add);
            RefreshCommand = new DelegateCommand(Refresh);
            SearchCommand = new DelegateCommand(Search);
            EditCommand = new DelegateCommand(Edit);
            DeleteCommand = new DelegateCommand(Delete);

            #endregion interface delegate command initialize
        }

        #region command function

        #region delegate command variable

        public DelegateCommand AddCommand { get; set; }
        public DelegateCommand RefreshCommand { get; set; }
        public DelegateCommand SearchCommand { get; set; }
        public DelegateCommand EditCommand { get; set; }
        public DelegateCommand DeleteCommand { get; set; }

        #endregion delegate command variable

        #region delegate command function

        private void Add()
        {
            DTO_adi_dic_drug drug = null;
            var parameters = new NavigationParameters();
            parameters.Add("adidrug", drug);
            {
                string navigatePath = "PUC_EditDrug";
                RegionManager.RequestNavigate(CGlobalRegionNames.MainContentRegion, navigatePath, parameters);
                Message = "进入添加药品界面";
            }
        }

        private void Refresh()
        {
            dicDrugs = _msDrugsService.GetDrugs(50);

            if (null == dicDrugs)
            {
                Message = "获取药品字典出现异常";
            }
            else
            {
                if (dicDrugs.Count > 0)
                {
                    vmDrugs = VMChange.GetVM(dicDrugs);
                }
                else
                {
                    vmDrugs.Clear();
                }
                Message = "获取数据完成";
            }
            LogMessage(Message);
        }

        private void Search()
        {
            dicDrugs = _msDrugsService.GetDrugs(drugCode, pyCode, drugName);
            if (dicDrugs != null)
            {
                if (dicDrugs.Count > 0)
                {
                    vmDrugs = VMChange.GetVM(dicDrugs);
                }
            }
            Message = "查询结束";
            LogMessage(Message);
        }

        private void Edit()
        {
            DrugSelected(selectVMDrug);
        }

        private void Delete()
        {
            if (MessageBoxResult.OK == MessageBox.Show("你确定要删除该药品信息吗?", "Confirm Message", MessageBoxButton.OKCancel))
            {
                List<DTO_adi_dic_drug> ld = _msDrugsService.SearchDrugsByDrugCode(selectVMDrug.DRUG_CODE);
                if (ld.Count == 1)
                {
                    _msDrugsService.DeleteDrug(ld[0]);
                }
                Message = "数据删除完成";
                LogMessage(Message);
                Refresh();
            }
        }

        private void DrugSelected(CVMDrug vmdrug)
        {
            List<DTO_adi_dic_drug> OBJs = _msDrugsService.SearchDrugsByDrugCode(vmdrug.DRUG_CODE);
            if (OBJs.Count == 1)
            {
                var parameters = new NavigationParameters();
                selectDicDrug = (OBJs[0]);
                parameters.Add("adidrug", selectDicDrug);
                if (selectDicDrug != null)
                {
                    string navigatePath = "PUC_EditDrug";
                    RegionManager.RequestNavigate(CGlobalRegionNames.MainContentRegion, navigatePath, parameters);
                }
            }
        }

        #endregion delegate command function

        #endregion command function
    }
}